package com.operate.service;

import com.operate.bean.VO.CustomNumVO;
import com.operate.bean.VO.OnlineOrderNumVO;
import com.operate.dao.ScreenDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/10/31 5:56
 */
@Slf4j
@Service
public class ScreenDataService {

    @Autowired
    private ScreenDataMapper screenMapper;


    public List<OnlineOrderNumVO> onlineOrderNumList() {
        return null;
    }

    /**
     * @name: customNum
     * @description: 客户数统计（近12个月）
     * @auther: ZeckZheng
     */
    public List<CustomNumVO> customNum() {
        List<CustomNumVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 11; i >= 0; i--) {
            String statTime = localDate.minusMonths(i).format(dtf);
            CustomNumVO customNumVO = new CustomNumVO();
            customNumVO.setStatTime(statTime);
            customNumVO.setCustomNum(0L);
            customNumVO.setActiveNum(0L);
            customNumVO.setTotal(0L);
            list.add(customNumVO);
        }

        //客户数统计（近12个月）
        Map<String, Map<String, Object>> customMap = screenMapper.customNum();
        //活跃客户数统计（近12个月）
        Map<String, Map<String, Object>> activeMap = screenMapper.activeCustomNum();


        Long total = (Long) customMap.get("BF").get("customNum");

        for (CustomNumVO customNumVO : list) {
            customNumVO.setTotal(total);
            if (customMap.get(customNumVO.getStatTime()) != null) {
                customNumVO.setCustomNum((Long) customMap.get(customNumVO.getStatTime()).get("customNum"));
            }
            if (activeMap.get(customNumVO.getStatTime()) != null) {
                customNumVO.setActiveNum((Long) activeMap.get(customNumVO.getStatTime()).get("statNum"));

            }
        }

        for (int i = 0; i < 12; i++) {
            Long num = 0L;
            for (int j = 0; j <= i; j++) {
                num = num + list.get(j).getCustomNum();
            }
            CustomNumVO vo = list.get(i);
            vo.setTotal(num + vo.getTotal());
        }
        return list;
    }

}









